home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16824 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  107 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!nickh
  3. From: nickh@netcom.com (Nick Huang)
  4. Subject: MS VC4.0 having problem with friendship + inheritance?
  5. Message-ID: <nickhDpq1xL.IC4@netcom.com>
  6. Keywords: friend, inheritance
  7. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  8. Date: Thu, 11 Apr 1996 23:44:09 GMT
  9. Sender: nickh@netcom17.netcom.com
  10.  
  11. I run into problem with the following program with
  12. MS VC4.0 (VC2.0 runs fine), which says one of the
  13. statement is ambiguous. Do you think it's 
  14. my problem or the compiler's problem?
  15.  
  16. Thanks a lot.
  17.  
  18. Nick
  19.  
  20. nickh@netcom.com
  21.  
  22. == test.hpp and test.cpp
  23.  
  24. // test.hpp
  25.  
  26. template <class T> class Base {
  27. protected:
  28.     T Babe;
  29. public:
  30.     Base();
  31.     Base(T);
  32.  
  33.     friend int operator == (Base<T>&, Base<T>&);
  34.     operator != (Base<T>&);
  35. };
  36.  
  37. template <class T> class Array : public Base<T> {
  38. protected:
  39.     T Val;
  40. public: 
  41.     Array();
  42.     Array(T);
  43.  
  44.     friend int operator == (Array<T>&, Array<T>&);
  45.     operator != (Array<T>&);
  46. };
  47.  
  48. // test.cpp
  49.  
  50. #include <stdio.h>
  51.  
  52. #include "test.hpp"
  53.  
  54. // Base
  55. template<class T> int operator == (Base<T>& x, Base<T>& y) 
  56. {
  57.     return x.Babe == y.Babe; 
  58. }
  59.  
  60. template<class T> int Base<T>::operator != (Base<T>& x)
  61. {
  62.     return this->Babe != x.Babe;
  63. }
  64.  
  65. template<class T> Base<T>::Base(T x)
  66. {
  67.     Babe = x;
  68. }
  69.  
  70. template<class T> Base<T>::Base()
  71. {
  72.  
  73. }
  74.  
  75. // Array 
  76.  
  77. template<class T> int operator == (Array<T>& x, Array<T>& y) 
  78. {
  79.     return x.Val == y.Val; 
  80. }
  81.  
  82. template<class T> int Array<T>::operator != (Array<T>& x)
  83. {
  84.     return this->Val != x.Val;
  85. }
  86.  
  87. template<class T> Array<T>::Array(T x)
  88. {
  89.     Val = x;
  90. }
  91.  
  92. template<class T> Array<T>::Array()
  93. {
  94.  
  95. }
  96.  
  97. main()
  98. {
  99.     Base<int>  m(3), n(2);
  100.     Array<int> x(3), y(2), a(4), b(4);
  101.  
  102.     if (m != n) printf("base not eq.\n");
  103.     if (x != y) printf("hello.\n");
  104.     if (a == b) printf("hello again\n"); // MSVC4.0 says this is ambiguous!!
  105.     return 0;
  106. }
  107.